/**
 * 品牌
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author duanzucai
 * @date 2018/11/10
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate', 'jquery', 'zmnSelect', 'zmnApp', 'zmnBind'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let table = layui.table;
    let laypage = layui.laypage;
    let laydate = layui.laydate;
    let zmnBind = layui.zmnBind;
    let zmnApp = layui.zmnApp;
    let zmnSelect = layui.zmnSelect;
    form.render(null, "zmn-form");
    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: [],
        page: true,
        limit: json.pageSize,
        height: 'full-225',
        done: function () {

        }
    });

    // 渲染日期范围选择器
    laydate.render({
        elem: '#timeSearch',
        range: true,
        min: -90,
        max: 0,
        format: 'yyyy/MM/dd',
        trigger: 'click'
    });

    laypage.render(zmnPage.laypageOptions(json));

    // 启用/停用/新增
    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event == "add") {
            ruleInfo('');
            return;
        }
        if (obj.event == 'threshold') {
            thresholdInfo();
            return;
        }
        if (obj.event == "start" || obj.event == "stop") {
            let ids = table.checkStatus('zmn-table').data.map((item) => item.thresholdId);
            modifyStatus(obj, ids);
        }
    });

    function initAreas(provinceId, cityId) {
        zmnSelect.selectlAreas({
            provinceSelectId: 'provinceId',
            citySelectId: 'cityId'
        }, {
            provinceSelectId: {
                selectedValues: provinceId
            },
            citySelectId: {
                selectedValues: cityId
            }
        }, {
            isRadio: true
        });
    }

    initAreas(json.provinceId, json.cityId);

    form.on("switch(chk_one_status)", function (obj) {
        let id = obj.elem.getAttribute("key");
        var ids = new Array();
        ids.push(id);
        let status = obj.elem.checked ? 2 : 1;
        var optObj = new Object();
        optObj.notConfirm = true;
        switch (status) {
            case 1:
                optObj.event = 'stop';
                modifyStatus(optObj, ids);
                break;
            case 2:
                optObj.event = 'start';
                modifyStatus(optObj, ids);
                break;
        }
    });

    function modifyStatus(obj, ids) {
        if (ids.length === 0) {
            return layer.alert("请选择至少一条数据！");
        }
        switch (obj.event) {
            case 'stop':
                var url = "/brs/rule/threshold/disable.action";
                if (obj.notConfirm != null) {
                    ajaxSimple(url, ids);
                } else {
                    layer.confirm('确定停用吗？', function () {
                        ajaxSimple(url, ids);
                    });
                }
                break;
            case 'start':
                var url = "/brs/rule/threshold/enable.action";
                if (obj.notConfirm != null) {
                    ajaxSimple(url, ids);
                } else {
                    layer.confirm('确定启用吗？', function () {
                        ajaxSimple(url, ids);
                    });
                }
                break;
        }
    }

    function ajaxSimple(url, ids) {
        $.ajax({
            type: "POST",
            url: url,
            dataType: "json",
            data: {
                ids: ids.join(",")
            },
            success: function (data) {
                if (data.status == 200) {
                    layui.layer.msg(data.message, {"icon": 1, "time": 1000}, function () {
                        location.reload();
                    });
                } else {
                    layui.layer.msg(data.message, {"icon": 2});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layui.layer.msg("状态修改异常！", {"icon": 2});
            }
        });
    }

    $(".zmn-form-cancel").click(function () {
        initAreas(0, 0);
        let obj = {
            "plat": ""
            , "orgThrId": ""
            , "timeSearch": ""
            , "status": ""
        };
        form.val('zmn-form', JSON.parse(JSON.stringify(obj)));
    });

    //对外暴露的接口
    exports('brs_order_rule_quantity_list', {});
});

function ruleInfo(thresholdId) {
    layui.layer.open({
        type: 2,
        title: '配置规则',
        area: ['520px', '430px'],
        content: '/brs/order/rule/quantity/addEditView.action?eventId=' + json.eventId + '&&thresholdId=' + thresholdId
    });
}

function thresholdInfo() {
    layui.layer.open({
        type: 2,
        title: '阈值配置',
        area: ['520px', '250px'],
        content: '/brs/order/rule/common/addEditView.action?eventId=' + json.eventId + '&&ruleId=' + json.ruleId
    });
}